package com.edu.eduservice.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.commonutils.R;
import com.edu.commonutils.Results;
import com.edu.eduservice.domain.CourseQuery;
import com.edu.eduservice.domain.EduChapter;
import com.edu.eduservice.domain.EduCourse;
import com.edu.eduservice.domain.EduCourseDescription;
import com.edu.eduservice.domain.view.CourseInfoVo;
import com.edu.eduservice.domain.view.CoursePublishVo;
import com.edu.eduservice.domain.view.CourseQueryVo;
import com.edu.eduservice.domain.view.CourseWebVo;
import com.edu.eduservice.mapper.EduCourseMapper;
import com.edu.eduservice.service.EduChapterService;
import com.edu.eduservice.service.EduCourseDescriptionService;
import com.edu.eduservice.service.EduCourseService;
import com.edu.eduservice.service.EduVideoService;
import com.edu.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2022-09-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述的注入
    @Autowired(required = false)
    private EduCourseDescriptionService courseDescriptionService;

    //课程的注入
    @Autowired(required = false)
    private EduCourseMapper eduCourseMapper;

    //小节注入 因为还需要删除小节里面的视屏用 service好点
    @Autowired
    private EduVideoService eduVideoService;

    //章节注入
    @Autowired(required = false)
    private EduChapterService eduChapterService;

    @Override
    public R addCourse(CourseInfoVo courseInfoVo) {
        //向课程表添加信息
        //把所有数据转成course对象再进行添加
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = eduCourseMapper.insert(eduCourse);
        if(insert ==0){
            try {
                throw new MyException(Results.ERROR,"添加课程失败");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
        //向课程描述表添加信息
        EduCourseDescription description=new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        description.setId(eduCourse.getId());
        courseDescriptionService.save(description);

        //返回数据
        R r = R.ok().data("courseid", eduCourse.getId());

        return r;
    }

    //根据编号查询 课程表和课程详细表数据
    @Override
    public R byid(String courseid) {
        //查询出课程详细表数据
        EduCourseDescription description = this.courseDescriptionService.getById(courseid);
        //想要统一返回就得用courseifovo这个类
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        //这个可以把属性值给名称相对应的属性里面
        //查询出课程表数据
        EduCourse eduCourse = this.eduCourseMapper.selectById(courseid);
        //赋值课程
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //赋值课程描述
        BeanUtils.copyProperties(description,courseInfoVo);
        //封装数据返回
        R r = R.ok().data("courseInfoVo", courseInfoVo);

        return r;
    }

    //修改课程详细和课程
    @Override
    public R updatecourse(CourseInfoVo courseInfoVo) {
        //向课程表修改信息
        EduCourse eduCourse=new EduCourse();
        //把courseinfovo数据给eduCourse
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = eduCourseMapper.updateById(eduCourse);
        //判断有没有修改成功
        if(update ==0){
            try {
                throw new MyException(Results.ERROR,"添加课程失败");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
        //向课程描述表添加信息
        EduCourseDescription description=new EduCourseDescription();
        //把courseinfovo数据给description
        BeanUtils.copyProperties(courseInfoVo,description);
        description.setId(eduCourse.getId());
        //修改
        courseDescriptionService.updateById(description);
        //封装返回数据
        R r = R.ok().data("courseid", eduCourse.getId());
        return r;
    }

    //根据编号返回发布消息
    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(id);
        return coursePublishVo;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();//标题
        String teacherId = courseQuery.getTeacherId();//讲师
        String subjectParentId = courseQuery.getSubjectParentId();//一级类型
        String subjectId = courseQuery.getSubjectId();//二级类型
        String status=courseQuery.getStatus();//是否发布

        if (!StringUtils.isEmpty(title)&&title!=null) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId)&&teacherId!=null ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)&&subjectParentId!=null) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)&&subjectId!=null) {
            queryWrapper.ge("subject_id", subjectId);
        }

        if (!StringUtils.isEmpty(status)&&status!=null) {
            queryWrapper.ge("status", status);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public void delCourse(String id) throws MyException {
        //删除小节 并且删除里面的视屏
            this.eduVideoService.delVideo(id);
        //删除章节
        QueryWrapper<EduChapter> queryWrapper01 =new QueryWrapper<>();
        queryWrapper01.eq("course_id",id);
        this.eduChapterService.remove(queryWrapper01);
        //删除描述
            this.courseDescriptionService.removeById(id);
        //删除课程本身
        int result = this.eduCourseMapper.deleteById(id);
        if(result <1)
            throw new MyException(Results.ERROR,"删除失败");

    }

    //根据讲师编号查询所讲课程 更新时间降序排列
    @Override
    public List<EduCourse> selectbyTeacher(String id) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //时间降序
        wrapper.orderByDesc("gmt_modified");
        wrapper.eq("teacher_id",id);
        List<EduCourse> courses = baseMapper.selectList(wrapper);

        return courses;
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        //条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseQuery.getSubjectParentId())){
            queryWrapper.eq("subject_parent_id",courseQuery.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseQuery.getSubjectId())){
            queryWrapper.eq("subject_id",courseQuery.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseQuery.getBuyCountSort())){
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
            //查询
        baseMapper.selectPage(pageParam, queryWrapper);

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("items", pageParam.getRecords());
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());

        return map;
    }

    //查询课程详细的方法
    @Override
    public CourseWebVo selectbyidCourseInfo(String id) {
        //修改浏览量
        this.updateByViewCount(id);

        return baseMapper.selectByidWeb(id);
    }

    //修改浏览量方法
    @Override
    public void updateByViewCount(String id) {
        //根据id查询课程
        EduCourse eduCourse = baseMapper.selectById(id);
        //修改课程的浏览数
        eduCourse.setViewCount(eduCourse.getViewCount()+1);
        //修改
        baseMapper.updateById(eduCourse);
    }
}
